package conceptmaniax.hotelapp.activities;

import java.text.ParseException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;
import conceptmaniax.hotelapp.adapters.RequestListAdapter;
import conceptmaniax.hotelapp.config.Constants;
import conceptmaniax.hotelapp.enums.Language;
import conceptmaniax.hotelapp.managers.RequestManager;
import conceptmaniax.hotelapp.util.GeneralUtils;

public class RequestListActivity extends Activity {
	private Language language;
	protected Button showInfo;
	protected Button showServices;
	protected Button showGuide;
	protected Button showBooking;
	protected FrameLayout homeScreen;
	protected TableLayout callout;
	protected ListView callout_list;
	protected TextView requestButton;
	protected TextView messageButton;
	protected TextView counter;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_requests);
		language = GeneralUtils.getLanguage(this);

		// Go back to HomePage Button
		final Button go_to_home = (Button) findViewById(R.id.homeButton);
		go_to_home.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
				final Intent homePageIntent = new Intent(
						Constants.HOMEPAGE_ACTIVITY);
				startActivity(homePageIntent);
			}
		});

		showInfo = (Button) findViewById(R.id.hotel_button);
		showServices = (Button) findViewById(R.id.services_button);
		showGuide = (Button) findViewById(R.id.guide_button);
		showBooking = (Button) findViewById(R.id.booking_button);
		homeScreen = (FrameLayout) findViewById(R.id.show_requests_main_layout);
		callout = (TableLayout) findViewById(R.id.callout_table);
		callout_list = (ListView) findViewById(R.id.callout_listview);
		requestButton = (TextView) findViewById(R.id.request_button);
		messageButton = (TextView) findViewById(R.id.message_button);
		counter = (TextView) findViewById(R.id.counter);

		requestButton.setSelected(true);
		requestButton.setEnabled(false);
		switch(language) {
		case ENGLISH:
			requestButton.setText("REQUESTS");
			messageButton.setText("MESSAGES");
			break;
		case GREEK:
			requestButton.setText("ΑΙΤΗΜΑΤΑ");
			messageButton.setText("ΜΗΝΥΜΑΤΑ");
			break;
		}
		fillRequests();
	}

	public void switchViews(View v) {
		ListView requestsLayout = (ListView) findViewById(R.id.request_layout);
		FrameLayout messagesLayout = (FrameLayout) findViewById(R.id.message_layout);
		if (requestButton.isSelected() && !messageButton.isSelected()) {
			requestButton.setSelected(false);
			requestButton.setEnabled(true);
			requestButton.setBackgroundColor(getResources().getColor(R.color.requests_button));
			messageButton.setSelected(true);
			messageButton.setEnabled(false);
			messageButton.setBackgroundColor(getResources().getColor(R.color.requests_button_selected));
			Animation a = AnimationUtils.loadAnimation(this, R.anim.hide);
			a.reset();
			requestsLayout.clearAnimation();
			requestsLayout.startAnimation(a);
			requestsLayout.setVisibility(View.INVISIBLE);
			a = AnimationUtils.loadAnimation(this, R.anim.in_from_left);
			a.reset();
			messagesLayout.clearAnimation();
			messagesLayout.startAnimation(a);
			messagesLayout.setVisibility(View.VISIBLE);
		} else if (!requestButton.isSelected() && messageButton.isSelected()) {
			messageButton.setSelected(false);
			messageButton.setEnabled(true);
			messageButton.setBackgroundColor(getResources().getColor(R.color.requests_button));
			requestButton.setSelected(true);
			requestButton.setEnabled(false);
			requestButton.setBackgroundColor(getResources().getColor(R.color.requests_button_selected));
			Animation a = AnimationUtils.loadAnimation(this, R.anim.hide);
			a.reset();
			messagesLayout.clearAnimation();
			messagesLayout.startAnimation(a);
			messagesLayout.setVisibility(View.INVISIBLE);
			a = AnimationUtils.loadAnimation(this, R.anim.in_from_right);
			a.reset();
			requestsLayout.clearAnimation();
			requestsLayout.startAnimation(a);
			requestsLayout.setVisibility(View.VISIBLE);
		}
	}

	public void fillRequests() {
		final ListView requestsLayout = (ListView) findViewById(R.id.request_layout);
		final RequestListAdapter adapter = new RequestListAdapter(this,
				RequestManager.getInstance().getRequests());
		Animation a = AnimationUtils.loadAnimation(this, R.anim.in_from_right);
		a.reset();
		requestsLayout.clearAnimation();
		requestsLayout.startAnimation(a);
		requestsLayout.setAdapter(adapter);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		SetButtonActivity.getInstance().setRecognition(false);
		SetButtonActivity.getInstance().setButtons(this, showInfo,
				showServices, showGuide, showBooking, callout, callout_list,
				homeScreen, null);
		try {
			int counter = RequestManager.getInstance().getRequestCount();
			this.counter.setText(""+counter);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    // manage message counter
	    if (this.counter.getText().equals("0")) 
	        this.counter.setVisibility(View.INVISIBLE);
	     else
	        this.counter.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindDrawables(findViewById(R.id.show_requests_relative));
	    System.gc();  
	}
	
	private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
        	view.getBackground().setCallback(null);
        }
        
        if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            	unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }
}